# -*- coding:utf-8 -*-
import random

from client import Client, DataBaseError
from fetcher import Fetcher
from main.util import debug, warning, dict_to_obj
import re
from bs4 import BeautifulSoup as BS
import time

from parseError import ParseError
from pojo.house import House

url_prefix = 'https://ts.lianjia.com/chengjiao'
client = None
table_communities = 'communities'
table_houses = 'houses'
# 从此ID继续
resume_hid = ''


def get_resume_hid_index(hid):
    table = client.get_collection(table_houses)
    result = [i["hid"] for i in table.find({}, {'hid': 1})]
    try:
        i_hid = result.index(hid)
    except ValueError:
        i_hid = 0
    return i_hid


def get_house_id_list():
    table = client.get_collection(table_houses)
    # 查找 没有详细信息的
    result = table.find({'model':''}, {'hid': 1})
    id_list = [r['hid'] for r in result]
    return id_list


def get_community_info_map():
    table = client.get_collection(table_communities)
    result = table.find({}, {'cid': 1, 'longitude': 1, 'latitude': 1})
    info_map = {}
    for r in result:
        info_map[r['cid']] = r['longitude'] != ''
    return info_map


def update_house(house_dict):
    house_t = client.get_collection(table_houses)
    line = house_t.update_one({'hid': house_dict['hid']}, {'$set': house_dict})
    print("更新房屋信息完成")


def update_community(cid, longitude, latitude):
    comms = client.get_collection(table_communities)
    com = comms.find_one({'cid': cid})
    com['longitude'] = longitude
    com['latitude'] = latitude
    comms.update_one({'cid': cid}, {'$set': com})
    print("更新小区坐标完成")


def check_database(collection, clean=False):
    if not client.has_collection(collection):
        warning(f"数据表 {collection} 不存在，新建")
    # 数据库非空确认
    table = client.get_collection(collection)
    if table.count_documents({}) > 0:
        if input(f"数据表 {collection} 非空！继续？(Y/N)") != 'Y':
            raise DataBaseError("终止操作")
    if clean:
        table.drop()
        debug("清空数据表 " + collection)


def parse_house(html, hid, info_map):
    soup = BS(html, "lxml")
    house_info = client.get_collection(table_houses).find({'hid': hid})[0]
    house = dict_to_obj(House, house_info)
    # 判断经纬度是否获取 并更新小区经纬度
    if not info_map[house.cid]:
        print("更新小区坐标")
        lng, lat = get_position(html)
        update_community(house.cid, lng, lat)
        info_map[house.cid] = True

    # 解析房屋信息
    get_house_info(soup, house)

    debug(house.__dict__, "房屋基本信息")
    return house.__dict__


def get_house_info(item, house):
    title = item.select('.house-title>.wrapper')[0]
    house.deal_time = title.span.text.split(' ')[0]

    house.price = item.select('.price>b')[0].text
    house.deal_value = item.select('.price>span>i')[0].text
    house.origin_value = item.select('.msg>:nth-child(1)>label')[0].text
    house.deal_duration = item.select('.msg>:nth-child(2)>label')[0].text

    base_info = item.select('.introContent .base')[0]
    house.model = base_info.select('.content>ul>:nth-child(1)')[0].contents[1].strip()
    house.floor = base_info.select('.content>ul>:nth-child(2)')[0].contents[1].strip()
    house.design_area = base_info.select('.content>ul>:nth-child(3)')[0].contents[1].strip()
    house.structure = base_info.select('.content>ul>:nth-child(4)')[0].contents[1].strip()
    house.real_area = base_info.select('.content>ul>:nth-child(5)')[0].contents[1].strip()
    house.building_type = base_info.select('.content>ul>:nth-child(6)')[0].contents[1].strip()
    house.face_direction = base_info.select('.content>ul>:nth-child(7)')[0].contents[1].strip()
    house.build_time = base_info.select('.content>ul>:nth-child(8)')[0].contents[1].strip()
    house.decoration = base_info.select('.content>ul>:nth-child(9)')[0].contents[1].strip()
    house.building_structure = base_info.select('.content>ul>:nth-child(10)')[0].contents[1].strip()
    house.lift_ratio = base_info.select('.content>ul>:nth-child(12)')[0].contents[1].strip()
    house.has_lift = base_info.select('.content>ul>:nth-child(13)')[0].contents[1].strip()

    house_features = item.select('#house_feature .showbasemore')
    if len(house_features) == 1:
        features = house_features[0].select('.baseattribute')
        for f in features:
            if f.select('.name')[0].text == '交通出行':
                house.desc_transportation = f.select('.content')[0].text.strip()
            if f.select('.name')[0].text == '周边配套':
                house.desc_surroundings = f.select('.content')[0].text.strip()


def get_position(html):
    pattern = 'resblockPosition:\'(.*?),(.*?)\','
    res = re.findall(pattern, html)
    if len(res) != 1:
        debug("获取位置失败", "获取小区位置")

    return res[0]


if __name__ == '__main__':

    """
        根据房屋，获取每个房屋详细信息
    """

    # 初始化
    client = Client()
    hid_list = get_house_id_list()
    fetcher = Fetcher(url_prefix)

    i_hid = get_resume_hid_index(resume_hid)
    com_info_map = get_community_info_map()

    size = len(hid_list)-i_hid

    # 获取
    for i,hid in enumerate(hid_list[i_hid:]):
        debug(hid, f"使用房屋ID {i}/{size} ")
        # 遍历小区ID
        html = fetcher.get_content3(hid)
        # 解析并更新
        try:
            update_house(parse_house(html, hid, com_info_map))
        except ParseError as e:
            e.print_reason()
            continue

        time.sleep(random.randint(1, 3))
